package com.abc.ytplayerdemo

import android.content.DialogInterface
import android.os.Bundle
import android.view.View
import android.widget.Button
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import com.mmdaz.www.mzsystemapi.MZSystemServiceHelper
import com.mmdaz.www.mzsystemapi.OnConnectListener2
import com.mmdaz.www.mzsystemapi.api.IMZSystemApi2
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
import java.util.*

class MainActivity : AppCompatActivity(),View.OnClickListener {

    private lateinit var myYouTubePlayer: MyYouTubePlayer
    private lateinit var youTubePlayerView: YouTubePlayerView

    var imzSystemApi2: IMZSystemApi2? = null
    private lateinit var mzSystemServiceHelper:MZSystemServiceHelper
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_youtobe)

        youTubePlayerView = findViewById(R.id.youtube_player_view)
        myYouTubePlayer = MyYouTubePlayer(youTubePlayerView)
        myYouTubePlayer.start("HEwtPwkeXjw")

        findViewById<View>(R.id.pause_button).setOnClickListener(this)
        findViewById<View>(R.id.quality_button).setOnClickListener(this)
        mzSystemServiceHelper = MZSystemServiceHelper.getInstance(this)
        mzSystemServiceHelper.setOnConnectListener(object : OnConnectListener2 {
            override fun onConnected(mzSystemApi: IMZSystemApi2) {
                imzSystemApi2 = mzSystemApi
            }

            override fun onDisconnected() {}
        })
        mzSystemServiceHelper.bindService()
        findViewById<View>(R.id.yb_button).setOnClickListener(this)
    }

    var flag = false
    override fun onClick(p0: View?) {
        when (p0!!.id) {
            R.id.pause_button -> if (myYouTubePlayer.isPlaying) {
                (p0 as Button).text = "暂停"
                myYouTubePlayer.pause()
            } else {
                (p0 as Button).text = "播放"
                myYouTubePlayer.resume()
            }
            R.id.quality_button -> showQualityDialog()
            R.id.yb_button -> {
                val api = imzSystemApi2
                if(api != null){
                    if(flag)
                        api.setProp("mmdaz.getbackmusic.set","on")
                    else
                        api.setProp("mmdaz.getbackmusic.set","off")
                    flag = !flag
                }
            }
        }
    }


    fun getSizeOfQuality(quality: String?): String? {
        when (quality) {
            "hd1080" -> return "1080p-hd"
            "hd720" -> return "720p"
            "large" -> return "480p"
            "medium" -> return "360p"
            "small" -> return "240p"
            "tiny" -> return "144p"
            "auto" -> return "自动"
        }
        return "未知"
    }

    private fun showQualityDialog() {
        val qualityLevels = myYouTubePlayer.getQualityLevels()
        if (qualityLevels == null || qualityLevels.size == 0) {
            return
        }
        val qualityLevels4Show = arrayOfNulls<String>(qualityLevels.size)
        for (i in qualityLevels.indices) {
            qualityLevels4Show[i] = String.format(
                "%s (%s)", getSizeOfQuality(qualityLevels[i]),
                qualityLevels[i]
            )
        }
        val sel = Arrays.binarySearch(qualityLevels, myYouTubePlayer.getCurQuality())
        val selectedItem = intArrayOf(sel)
        val builder = AlertDialog.Builder(this)
        builder.setTitle("画质选择")
            .setSingleChoiceItems(
                qualityLevels4Show, selectedItem[0]
            ) { dialog: DialogInterface, which: Int ->
                myYouTubePlayer.setPlaybackQuality(qualityLevels[which])
                dialog.dismiss()
            }
        val dialog = builder.create()
        dialog.show()
    }
}